'''
中间件模块
'''

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
import time

class AppMiddleware(MiddlewareMixin):

    def process_request(self,request):
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())+" [myapp] procces_request:"+str(request))

    def process_view(self, request, view_func, view_args, view_kwargs):
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " [myapp] procces_view:"+str(view_func))

        # 如果process_view设置了返回值，将不执行视图的index方法，而是直接返回页面
        # return HttpResponse("myapp:process_view")


    def process_exception(self,request,exception):
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " [myapp] procces_exception:"+str(exception))
        return HttpResponse(str(exception))

    def process_response(self,request,response):
        print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) +"[myapp] procces_response:"+str(response))
        return response
